/*
 * @(#)AbstractPersistableModel.java
 * Date 2013-08-30
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.model.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.codehaus.jackson.annotate.JsonIgnore;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.hornerscorners.lamppost.exception.ServiceActionError;
import us.hornerscorners.lamppost.exception.ServiceActionException;
import us.hornerscorners.lamppost.exception.SeverityType;
import us.hornerscorners.lamppost.transformer.IJsonMarshaller;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-08-30
 * @author         Jim Horner
 */
public abstract class AbstractPersistableModel extends AbstractClassifiableModel
        implements IPersistableModel {

    /** Field description */
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /** Field description */
    @JsonIgnore
    private final PersistableModelType modelType;

    /** Field description */
    @JsonIgnore
    private byte[] previousVersion;

    /** Field description */
    private long version;

    /**
     * Constructs ...
     *
     */
    public AbstractPersistableModel(PersistableModelType modelType) {

        super();

        this.version = 0;
        this.modelType = modelType;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public PersistableModelType getModelType() {

        return this.modelType;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public byte[] getPreviousVersion() {
        return previousVersion;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public long getVersion() {
        return version;
    }

    /**
     * Method description
     *
     */
    @Override
    public void incrementVersion() {

        ++this.version;
    }

    /**
     * Method description
     *
     *
     * @param marshaller
     * @throws us.hornerscorners.lamppost.exception.ServiceActionException
     */
    public void saveStateToPreviousVersion(IJsonMarshaller marshaller)
            throws ServiceActionException {

        try {

            ByteArrayOutputStream bas = new ByteArrayOutputStream();

            marshaller.writeValue(bas, this);
            
            this.previousVersion = bas.toByteArray();

        } catch (IOException e) {

            logger.error("Error", e);

            ServiceActionError error = new ServiceActionError("ERROR",
                                           "Unable to create JSON for previous version.",
                                           SeverityType.Error);

            throw new ServiceActionException(getClass(), error);
        }
    }

    /**
     * Method description
     *
     *
     * @param previousVersion
     */
    public void setPreviousVersion(byte[] previousVersion) {
        this.previousVersion = previousVersion;
    }

    /**
     * Method description
     *
     *
     * @param version
     */
    public void setVersion(long version) {
        this.version = version;
    }
}
